<?php
namespace api\modules\v1\controllers;

use api\components\ApiController;
use api\modules\v1\models\MessagePrompt;
use api\modules\v1\models\UserInfo;
use Yii;

/**
 * 账户控制器
 * @author WJ
 */
class UserController extends ApiController
{
    public $defaultAction = 'user-login';

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['authenticator']['optional'] = ['login', 'admin-login', 'register', 'password-set','password-reset'];
        return $behaviors;
    }

    /**
     * 用户注册
     * @author zcy
     */
    public function actionRegister()
    {
        $params = Yii::$app->request->post();
        $model = new UserInfo();
        $model->setScenario(UserInfo::SCENARIO_USER_REGISTER);
        $model->load($params, '');
        if ($result = $model->register()) {
            return ['message' => '用户注册成功', 'data' => $result];
        }

    }

    /**
     * 用户登录
     * @author wj
     */
    public function actionLogin()
    {
        $params = Yii::$app->request->post();
        $model = new UserInfo();
        $model->setScenario(UserInfo::SCENARIO_USER_LOGIN);
        $model->load($params, '');
        if ($result = $model->login()) {
            return ['message' => '用户登陆成功', 'data' => $result];
        }
    }

    /**
     * 管理员和平台登录
     * @author wj
     */
    public function actionAdminLogin()
    {
        $params = Yii::$app->request->post();
        $model = new UserInfo();
        $model->setScenario(UserInfo::SCENARIO_ADMIN_LOGIN);
        $model->load($params, '');
        if ($result = $model->login()) {
            return ['message' => '登陆成功', 'data' => $result];
        }
    }

    /**
     * 获取真实姓名
     * @author wj
     */
    public function actionGetName()
    {
        $model = new UserInfo();
        if ($result = $model->getUserName()) {
            return ['message' => '用户个人资料获取成功', 'data' => $result];
        }
    }

    /**
     * 用户个人资料
     * @author wj
     */
    public function actionUserData()
    {
        $model = new UserInfo();
        if ($result = $model->getUserData()) {
            return ['message' => '用户个人资料获取成功', 'data' => $result];
        }
    }

    /**
     * 用户登录ip列表
     * @author wj
     */
    public function actionLoginHistory()
    {
        $params = Yii::$app->request->post();
        $model = new UserInfo();
        $model->setScenario(UserInfo::SCENARIO_LOGIN_HISTORY);
        $model->load($params, '');
        if ($result = $model->ipList()) {
            return ['message' => '登陆IP列表获取成功', 'data' => $result];
        }
    }

    /**
     * 获取提示消息列表
     * @author wj
     */
    public function actionMessageList()
    {
        $params = Yii::$app->request->post();
        $model = new MessagePrompt();
        $model->setScenario(MessagePrompt::SCENARIO_LIST);
        $model->load($params, '');
        if ($result = $model->messageList()) {
            return ['message' => '提示记录列表获取成功', 'data' => $result];
        }
    }

    /**
     * 设置提示记录已读
     * @author wj
     */
    public function actionMessageDefault()
    {
        $params = Yii::$app->request->post();
        $model = new MessagePrompt();
        $model->setScenario(MessagePrompt::SCENARIO_SET_READ);
        $model->load($params, '');
        if ($result = $model->messageDefault()) {
            return ['message' => '消息标为已读操作成功', 'data' => $result];
        }
    }

    /**
     * 修改登录密码
     * @author zcy
     */
    public function actionPasswordEdit()
    {
        $params = Yii::$app->request->post();
        $model = new UserInfo();
        $model->setScenario(UserInfo::SCENARIO_PASSWORDEDIT);
        $model->load($params, '');
        if ($result = $model->editPassword()) {
            return ['message' => '密码修改成功'];
        }

    }

    /**
     * 修改邮箱
     * @author zcy
     */
    public function actionEmailEdit()
    {

        $params = Yii::$app->request->post();
        $model = new UserInfo();
        $model->setScenario(UserInfo::SCENARIO_EDITEMAIL);
        $model->load($params, '');
        if ($result = $model->editEmail()) {
            return ['message' => '邮箱修改成功', 'data' => $result];
        }

    }

    /**
     * 修改手机号码
     * @return json
     */
    public function actionMobileEdit()
    {
        $params = Yii::$app->request->post();
        $model = new UserInfo();
        $model->setScenario(UserInfo::SCENARIO_EDITMOBILE);
        $model->load($params, '');
        if ($result = $model->editMobile()) {
            return ['message' => '手机号码修改成功', 'data' => $result];
        }

    }

    /**
     * 设置邮箱
     * @return json
     */
    public function actionEmailSet()
    {
        $params = Yii::$app->request->post();
        $model = new UserInfo();
        $model->setScenario(UserInfo::SCENARIO_SETEMAIL);
        $model->load($params, '');
        if ($result = $model->setEmail()) {
            return ['message' => '邮箱绑定成功', 'data' => $result];
        }

    }

    /**
     * 设置手机号码
     * @return json
     */
    public function actionMobileSet()
    {
        $params = Yii::$app->request->post();
        $model = new UserInfo();
        $model->setScenario(UserInfo::SCENARIO_SETMOBILE);
        $model->load($params, '');
        if ($result = $model->setMobile()) {
            return ['message' => '手机号码绑定成功', 'data' => $result];
        }

    }

    /**
     * 设置交易密码
     * @return json
     */
    public function actionPaywordSet()
    {
        $params = Yii::$app->request->post();
        $model = new UserInfo();
        $model->setScenario(UserInfo::SCENARIO_SETPAYWORD);
        $model->load($params, '');
        if ($result = $model->setPayword()) {
            return ['message' => '交易密码设置成功'];
        }

    }

    /**
     * 修改交易密码
     * @author zcy
     */
    public function actionPaywordEdit()
    {
        $params = Yii::$app->request->post();
        $model = new UserInfo();
        $model->setScenario(UserInfo::SCENARIO_PAYWORDEDIT);
        $model->load($params, '');
        if ($result = $model->editPayword()) {
            return ['message' => '交易密码修改成功'];
        }

    }

    /**
     * 设置登录密码(邮箱找回)
     * @return json
     */
    public function actionPasswordSet()
    {
        $params = Yii::$app->request->post();
        $model = new UserInfo();
        $model->setScenario(UserInfo::SCENARIO_SETPASSWORD);
        $model->load($params, '');
        if ($result = $model->setPassword()) {
            return ['message' => '登录密码设置成功'];
        }

    }
    
    /**
     * 重设置登录密码(手机找回)
     * @author zcy
     */
    public function actionPasswordReset()
    {
        $params = Yii::$app->request->post();
        $model = new UserInfo();
        $model->setScenario(UserInfo::SCENARIO_RESETPASSWORD);
        $model->load($params, '');
        if ($result = $model->resetPassword()) {
            return ['message' => '登录密码设置成功'];
        }
    }
    
    /**
     * 用户实名认证
     */
    public function actionCertificate(){
        $params = Yii::$app->request->post();
        $model = new UserInfo();
        $model->setScenario(UserInfo::SCENARIO_CERTIFICATE);
        $model->load($params, '');
        if ($result = $model->certificate()) {
            return ['message' => '实名认证成功'];
        }
    }
    

}
